﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using TTQT.TTQT_Code;

/// <summary>
/// Summary description for SanPham_DatHang
/// </summary>
public class SanPham_DatHangDAO
{

    private string id = "";
    private string hoTen = "";
    private string gioiTinh = "";
    private string diaChi = "";
    private string email = "";
    private string dienThoai = "";
    private string diDong = "";
    private string fax = "";
    private string hoTen1 = "";
    private string gioiTinh1 = "";
    private string diaChi1 = "";
    private string email1 = "";
    private string dienThoai1 = "";
    private string diDong1 = "";
    private string fax1 = "";
    private string ghiChu1 = "";
    private string hinhThucVanChuyen = "";
    private string thoiGianVanChuyen = "";
    private string hinhThucThanhToan = "";
    private string trangThai = "";
    private string ngayTao = "";
    private string ghiChuXuLy = "";

    private string tongTien = "";
    private string iP = "";

    public int insertDetail(string donHangId, ArrayList spList)
    {
        int kq = 0;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        try
        {
            conn.Open();
            cmd.Connection = conn;
            foreach (SanPhamInfo info in spList)
            {
                cmd.CommandText = @"insert into sanpham_datHang_ChiTiet(
                donHangId,SanPhamId,SoLuong,Seri,NgayNhap,MaCN,TenHang,PicUrl,
                GhiChu,MaVach,GiaNhap,GiaBanThamKhao,ChietKhau,GiaBanCoChietKhau,GiaBanThucTe,UserName,Del,HangMoi,DaBan,
                active,NoiDung,TomLuoc,LoaiId,NhomId,parentId,spHot,banChay,NoiSXID,ThuongHieu
                )";
                cmd.CommandText += " select " + donHangId + "," + info.Id + "," + info.SoLuongDatMua;
                cmd.CommandText += @",Seri,NgayNhap,MaCN,TenHang,PicUrl,GhiChu,MaVach,GiaNhap,GiaBanThamKhao,ChietKhau,GiaBanCoChietKhau,
                GiaBanThucTe,UserName,Del,HangMoi,DaBan,active,NoiDung,TomLuoc,LoaiId,NhomId,parentId,spHot,banChay,
                NoiSXID,ThuongHieu
                from SanPham where id=";
                cmd.CommandText += info.Id;
                kq = cmd.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            kq = 0;
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }


        return kq;
    }
    public int insert()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_DatHang_Add", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@HoTen", HoTen);
        cmd.Parameters.AddWithValue("@GioiTinh", GioiTinh);
        cmd.Parameters.AddWithValue("@DiaChi", DiaChi);
        cmd.Parameters.AddWithValue("@Email", Email);
        cmd.Parameters.AddWithValue("@DienThoai", DienThoai);
        cmd.Parameters.AddWithValue("@DiDong", DiDong);
        cmd.Parameters.AddWithValue("@Fax", Fax);
        cmd.Parameters.AddWithValue("@HoTen1", HoTen1);
        cmd.Parameters.AddWithValue("@GioiTinh1", GioiTinh1);
        cmd.Parameters.AddWithValue("@DiaChi1", DiaChi1);
        cmd.Parameters.AddWithValue("@Email1", Email1);
        cmd.Parameters.AddWithValue("@DienThoai1", DienThoai1);
        cmd.Parameters.AddWithValue("@DiDong1", DiDong1);
        cmd.Parameters.AddWithValue("@Fax1", Fax1);
        cmd.Parameters.AddWithValue("@GhiChu1", GhiChu1);
        cmd.Parameters.AddWithValue("@HinhThucVanChuyen", HinhThucVanChuyen);
        cmd.Parameters.AddWithValue("@ThoiGianVanChuyen", ThoiGianVanChuyen);
        cmd.Parameters.AddWithValue("@HinhThucThanhToan", HinhThucThanhToan);
        cmd.Parameters.AddWithValue("@TongTien", TongTien);
        cmd.Parameters.AddWithValue("@IP", IP);
        

        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteScalar(), 0);
            Id = kq.ToString();
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public bool getById(string _id)
    {
        bool kq = false;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_DatHang_GetById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", _id);
        try
        {
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read())
            {
                kq = true;
                Id = TTQTCommon.ToString(dr["Id"]);
                HoTen = TTQTCommon.ToString(dr["HoTen"]);
                GioiTinh = TTQTCommon.ToString(dr["GioiTinh"]);
                DiaChi = TTQTCommon.ToString(dr["DiaChi"]);
                Email = TTQTCommon.ToString(dr["Email"]);
                DienThoai = TTQTCommon.ToString(dr["DienThoai"]);
                DiDong = TTQTCommon.ToString(dr["DiDong"]);
                Fax = TTQTCommon.ToString(dr["Fax"]);
                HoTen1 = TTQTCommon.ToString(dr["HoTen1"]);
                GioiTinh1 = TTQTCommon.ToString(dr["GioiTinh1"]);
                DiaChi1 = TTQTCommon.ToString(dr["DiaChi1"]);
                Email1 = TTQTCommon.ToString(dr["Email1"]);
                DienThoai1 = TTQTCommon.ToString(dr["DienThoai1"]);
                DiDong1 = TTQTCommon.ToString(dr["DiDong1"]);
                Fax1 = TTQTCommon.ToString(dr["Fax1"]);
                GhiChu1 = TTQTCommon.ToString(dr["GhiChu1"]);
                HinhThucVanChuyen = TTQTCommon.ToString(dr["HinhThucVanChuyen"]);
                ThoiGianVanChuyen = TTQTCommon.ToString(dr["ThoiGianVanChuyen"]);
                HinhThucThanhToan = TTQTCommon.ToString(dr["HinhThucThanhToan"]);

                TrangThai = TTQTCommon.ToString(dr["TrangThai"]);
                NgayTao = TTQTCommon.ToString(dr["NgayTao"]);
                GhiChuXuLy = TTQTCommon.ToString(dr["GhiChuXuLy"]);

                TongTien = TTQTCommon.ToString(dr["TongTien"]);

            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    /// <summary>
    /// Kq tra ve >0 la thanh cong
    /// </summary>
    /// <param name="_id"></param>
    /// <param name="_trangThai"></param>
    /// <param name="_GhiChuXuLy"></param>
    /// <returns></returns>
    public int updateGhiChu(string _id, string _GhiChuXuLy)
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_DatHang_Update", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", _id);
        cmd.Parameters.AddWithValue("@GhiChuXuLy", _GhiChuXuLy);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public int delete()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_DatHang_Delete", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id", Id);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    
    public DataTable getByTrangThai(string _trangthai)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_DatHang_selectByTrangThai", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@TrangThai", _trangthai);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public DataTable getChiTietDatHang(string donHangId)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_DatHang_GetChiTietHang", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@DonHangId", donHangId);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public int getId()
    {
        return TTQTCommon.ToInt32(id, -1);
    }
    public int updateTrangThai(string _id,string _trangThai)
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_DatHang_UpdateTrangThai", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", _id);
        cmd.Parameters.AddWithValue("@trangThai", _trangThai);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    #region Properties

    public string Id
    {
        get { return id; }
        set { id = value; }
    }

    public string HoTen
    {
        get { return hoTen; }
        set { hoTen = value; }
    }

    public string GioiTinh
    {
        get { return gioiTinh; }
        set { gioiTinh = value; }
    }

    public string DiaChi
    {
        get { return diaChi; }
        set { diaChi = value; }
    }

    public string Email
    {
        get { return email; }
        set { email = value; }
    }

    public string DienThoai
    {
        get { return dienThoai; }
        set { dienThoai = value; }
    }

    public string DiDong
    {
        get { return diDong; }
        set { diDong = value; }
    }

    public string Fax
    {
        get { return fax; }
        set { fax = value; }
    }

    public string HoTen1
    {
        get { return hoTen1; }
        set { hoTen1 = value; }
    }

    public string GioiTinh1
    {
        get { return gioiTinh1; }
        set { gioiTinh1 = value; }
    }

    public string DiaChi1
    {
        get { return diaChi1; }
        set { diaChi1 = value; }
    }

    public string Email1
    {
        get { return email1; }
        set { email1 = value; }
    }

    public string DienThoai1
    {
        get { return dienThoai1; }
        set { dienThoai1 = value; }
    }

    public string DiDong1
    {
        get { return diDong1; }
        set { diDong1 = value; }
    }

    public string Fax1
    {
        get { return fax1; }
        set { fax1 = value; }
    }

    public string GhiChu1
    {
        get { return ghiChu1; }
        set { ghiChu1 = value; }
    }

    public string HinhThucVanChuyen
    {
        get { return hinhThucVanChuyen; }
        set { hinhThucVanChuyen = value; }
    }

    public string ThoiGianVanChuyen
    {
        get { return thoiGianVanChuyen; }
        set { thoiGianVanChuyen = value; }
    }

    public string HinhThucThanhToan
    {
        get { return hinhThucThanhToan; }
        set { hinhThucThanhToan = value; }
    }

    public string TrangThai
    {
        get { return trangThai; }
        set { trangThai = value; }
    }

    public string NgayTao
    {
        get { return ngayTao; }
        set { ngayTao = value; }
    }

    public string GhiChuXuLy
    {
        get { return ghiChuXuLy; }
        set { ghiChuXuLy = value; }
    }

    public string TongTien
    {
        get { return tongTien; }
        set { tongTien = value; }
    }
    public string IP
    {
        get { return iP; }
        set { iP = value; }
    }
    #endregion
}
